﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Zhp.Common.Extention
{
    public static class EnumExt
    {
        /// <summary>
        /// 根据传入的int返回对应枚举属性名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumName<T>(this int value)
        {
            return Enum.Parse(typeof(T), Enum.GetName(typeof(T), value)).ToString();
        }

        /// <summary>
        /// 根据传入的int返回对应枚举描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDescription<T>(this int value)
        {
            return ToEnum<T>(value).GetDescription();
        }

        /// <summary>
        /// 根据传入的枚举属性获得对应值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int GetEnumValue<T>(this string name)
        {
            return Convert.ToInt32(Enum.Parse(typeof(T), name));
        }

        /// <summary>
        /// 根据枚举的name获取枚举
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="name">可能的枚举值</param>
        /// <returns>枚举,示例：enum1.en1</returns>
        public static TEnum ToEnum<TEnum>(this string name)
        {
            return (TEnum)Enum.Parse(typeof(TEnum), name);
        }

        /// <summary>
        /// 根据枚举的value获取枚举
        /// </summary>
        /// <param name="TEnum">枚举的类型</param>
        /// <param name="value">可能的枚举值</param>
        /// <returns>枚举,示例：enum1.en1</returns>
        public static Enum ToEnum<TEnum>(this int value)
        {
            return Enum.ToObject(typeof(TEnum), value) as Enum;
        }

        /// <summary>
        /// 从枚举中获取Description
        /// </summary>
        /// <param name="enumName">需要获取枚举描述的枚举</param>
        /// <returns>描述内容</returns>
        public static string GetDescription(this Enum enumName)
        {
            var fieldInfo = enumName.GetType().GetField(enumName.ToString());
            return fieldInfo.GetDescriptionFirst();
        }

        public static string GetDescriptionFirst(this FieldInfo fieldInfo)
        {
            return fieldInfo.GetDescriptAttr().GetDescriptionFirst();
        }

        /// <summary>
        /// 获取字段Description
        /// </summary>
        /// <param name="fieldInfo">FieldInfo</param>
        /// <returns>DescriptionAttribute[] </returns>
        public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
        {
            if (fieldInfo != null)
            {
                return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            }
            return null;
        }

        public static string GetDescriptionFirst(this DescriptionAttribute[] attributes)
        {
            var description = string.Empty;
            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            return description;
        }

        /// <summary>
        /// 获取枚举所有名称
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>枚举名称列表</returns>
        public static List<string> GetEnumNames<TEnum>()
        {
            return Enum.GetNames(typeof(TEnum)).ToList();
        }

        /// <summary>
        /// 获取所有枚举对应的值
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>枚举值列表</returns>
        public static List<int> GetEnumValues<TEnum>()
        {
            var list = new List<int>();
            foreach (var value in Enum.GetValues(typeof(TEnum)))
            {
                list.Add(Convert.ToInt32(value));
            }
            return list;
        }

        /// <summary>
        /// 获取枚举名以及对应的Description
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>返回Dictionary  ,Key为枚举名，  Value为枚举对应的Description</returns>
        public static Dictionary<object, string> GetNameAndDescription<TEnum>()
        {
            var dic = new Dictionary<object, string>();
            var type = typeof(TEnum);
            if (type.IsEnum)
            {
                var enumValues = Enum.GetValues(type);
                foreach (Enum value in enumValues)
                {
                    dic.Add(value, GetDescription(value));
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取枚举名以及对应的Value
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>返回Dictionary  ,Key为描述名，  Value为枚举对应的值</returns>
        public static Dictionary<string, int> GetDescriptionAndValue<TEnum>()
        {
            var dic = new Dictionary<string, int>();
            var type = typeof(TEnum);
            if (type.IsEnum)
            {
                var enumValues = Enum.GetValues(type);
                foreach (Enum value in enumValues)
                {
                    dic.Add(GetDescription(value), value.GetHashCode());
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取枚举字段
        /// </summary>
        /// <param name="apiEnumType"></param>
        /// <returns></returns>
        public static FieldInfo[] GetEnumFields(this Type apiEnumType)
        {
            return apiEnumType.GetFields(BindingFlags.Public | BindingFlags.Static);
        }
    }
}
